'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ChatIndexCtrl
 * @description
 * # ChatIndexCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('ChatIndexCtrl', function ($scope, $timeout, focus, DataService, ContactInfo,RongYService, openApi, ChatUser, ChatStatus, RongYapi, Groups, newMessageEventService, $window, $location, RongUtil, Util) {
        var vm = this
        this.goChatRoom = function (targetId, conversationType) {
            $location.path("/chat/chatroom/" + targetId + "/" + conversationType)
        };
        this.curTab = 1
        this.switchTab = function (type) {
            var path;
            switch (type) {
                case 1:
                    vm.curTab = 1
                    break;
                case 2:
                    vm.curTab = 2
                    break;
            }
        }
        this.createDiscussConfirm = function () {
            ChatStatus.setChatType(ChatStatus.PRIVATE);
            ChatStatus.setStatus(ChatStatus.CREATE);
            $location.path("/chat/selectuser")
        };
        this.showSearch = function () {
            vm.isShowSearch = true;
            focus("searchInput")
        };
        vm.hidesearch = function () {
            $timeout(function () {
                vm.isShowSearch = false;
                vm.search = "";
            }, 50);
        };
        this.goPrivate = function (id) {
            RongYService.goPravite(id)
        };
        this.hidemore = function () {
            vm.isShowMore = false;
        }
        this.showMore = function () {
            vm.isShowMore = !vm.isShowMore;
            this.showdel = false
        };
        this.removeUsers = function () {
            this.showdel = !this.showdel
        }
        this.itemOnLongPress = function (index, curconversiton) {
            if (navigator.notification) {
                navigator.notification.confirm(
                    '是否删除',
                    vm.checkConfirm(index, curconversiton),
                    '提示', ['删除', '取消']
                )
            }
        }
        this.checkConfirm = function (index, curconversiton) {
            var msg = vm.friends[index];
            return function (btn) {
                if (btn == 1) {
                    RongYService.removeConversation(curconversiton).then(function () {
                        vm.friends.splice(index, 1);
                    })
                }
            }
        };
        var newMsgCallBack = function (d, data) {
            vm.friends = vm.friends || [];
            var jsonMsg = JSON.parse(data);
            jsonMsg.unreadMessageCount = "1";
            var target;
            if (jsonMsg.conversationType == "PRIVATE") {
                target = ChatUser.get(jsonMsg.targetId);
            } else if (jsonMsg.conversationType == "GROUP") {
                target = Groups.get(jsonMsg.targetId);
            }
            jsonMsg = RongUtil.resolveCon(jsonMsg, 1, target);
            var index = findInFriends(jsonMsg.targetId);
            if (index > -1) {
                var unreadMessageCount = parseInt(vm.friends[index].unreadMessageCount);
                unreadMessageCount++;
                vm.friends[index].unreadMessageCount = unreadMessageCount;
                vm.friends[index].latestMessage = jsonMsg.latestMessage;
            } else {
                vm.friends.push(jsonMsg);
            }
        }
        newMessageEventService.listen(newMsgCallBack);

        var findInFriends = function (val) {
            for (var i = 0; i < vm.friends.length; i++) {
                if (vm.friends[i].targetId == val) {
                    return i;
                }
            }
            return -1;
        }

        var getConversationList = function () {
            vm.friends = vm.friends || [];
            RongYService.getConversationList().then(function (data) {
                var result = data.result;
                var target;
                for (var i = 0; i < result.length; i++) {
                    if (result[i].conversationType == "PRIVATE") {
                        target = ChatUser.get(result[i].targetId);
                    } else if (result[i].conversationType == "GROUP") {
                        target = Groups.get(result[i].targetId);
                    }
                    result[i] = RongUtil.resolveCon(result[i], 0, target);
                }
                vm.friends = result;

            })
        }
        var init = function () {
            ContactInfo.getData()
            getConversationList();
        }
        if (ChatUser.isEmpty()) {
            DataService.getData(openApi.getuserinfo).then(function (data) {
                ChatUser.set(data);
                init();
            });
        } else {
            init();
        }
        RongYapi.getMyGroups().then(function (ret, err) {
            if (ret) {
                vm.groups = ret.data.result;
                Groups.set(ret.data.result);
                console.log(vm.groups)
            }
        });

    });
